<?php

/**
 * category model
 * 
 * @author HungPV <phamvanhung0818@gmail.com>
 * @package category
 * @since 1.0
 */
class category extends CI_Model {

    function __construct() {
        parent::__construct();
        $this->load->model('building/building');
    }

    function get_category_spots($id) {
        $this->db->select('category_spot');
        $this->db->from('category');
        $this->db->where('category_id', $id);
        $query = $this->db->get();

        $result = $query->result();
        $array_building = array();
        if (count($result) > 0) {
            $spot = $result[0]->category_spot;
            if (strlen($spot) > 0) {
                $arr = explode("@", $spot);
                foreach ($arr as $item) {
                    $building = $this->building->get_building_spot_info($item);
                    if (isset($building)) {
                        $array_building[] = $building;
                    }
                }
            }
        }

        return $array_building;
    }

    function get_count() {
        return $this->db->count_all('category');
    }

    function get($id) {
        $query = $this->db->get_where('category', array('category_id' => $id));
        $result = $query->result();

        if (count($result) > 0) {
            return $result[0];
        } else {
            return NULL;
        }
    }

    function get_categories($limit = -1, $ofset = 0) {
        if ($limit == -1) {
            $query = $this->db->get('category');
        } else {
            $query = $this->db->get('category', $ofset, $limit);
        }
        return $query->result();
    }

    function add($name, $description, $spot = '') {
        $data = array(
            'category_name' => $name,
            'category_description' => $description,
            'category_spot' => $spot
        );

        $this->db->insert('category', $data);
        if ($this->db->affected_rows() > 0) {
            return true;
        } else {
            return false;
        }
    }

    function edit($id, $name, $description, $related = '') {
        $data = array(
            'category_name' => $name,
            'category_description' => $description,
            'category_spot' => $related
        );
        $this->db->where('category_id', $id);
        $this->db->update('category', $data);
    }

    function delete($id) {
        $this->db->delete('category', array('category_id' => $id));
    }

}

?>
